<template>
  <Header :title="{ zh: '配套仓目视管理看板', en: 'Cutting Warehouse Visual Board', index: 0 }" />
  <div class="EchartsContainer">
    <div class="row">
      <div class="item" style="width: 50%">
        <div class="title">库存状况</div>
        <div class="echarts" id="main"></div>
      </div>
      <div class="item" style="width: 50%">
        <div class="title">满单状况</div>
        <div class="echarts" id="main2"></div>
      </div>
    </div>

    <div class="row" style="margin-top: 10px;">
      <swiper :autoplay="{
        delay: 60000
      }" :slides-per-view="1" :space-between="2000" style="height: 100%; width: 100%">
        <swiper-slide>
          <div class="item">
            <div class="title" style="text-align: start">配套订单明细</div>
            <el-table ref="tab" border class="table" :data="tableData" style="width: 100%; height: 100%">
              <el-table-column align="center" prop="brand" label="品牌" />
              <el-table-column align="center" prop="lotNo" label="指令号" />
              <el-table-column align="center" prop="photoPath" label="鞋图 ">
                <template v-slot="{ row, $index }">
                  <el-image hide-on-click-modal preview-teleported style="height: 34px" :src="row.photoPath"
                    :zoom-rate="1.2" @click="imgClicked(row.photoPath, srcList)" :preview-src-list="srcList"
                    :initial-index="imgIndex" fit="cover" />
                </template>
              </el-table-column>
              <el-table-column align="center" prop="orderQty" label="订单数量" />
              <el-table-column align="center" prop="suppQty" label="预补数" />
              <el-table-column align="center" prop="finQty" label="已配套数" />
              <el-table-column align="center" prop="unFinQty" label="欠数" />
              <el-table-column align="center" prop="finQty" label="在仓库存数" />
              <el-table-column align="center" prop="fullFlag" label="满单状态" />
            </el-table>
          </div>
        </swiper-slide>
      </swiper>
    </div>
  </div>

</template>


<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import Header from '../components/Header.vue'
import * as echarts from 'echarts'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'
import { Swiper, SwiperSlide } from 'swiper/vue'
let rollTimer = ref() as any
const tab = ref<HTMLElement>() as any
const tab2 = ref<HTMLElement>() as any
const autoRoll = (stop: any) => {
  if (stop) {
    clearInterval(rollTimer.value)
    return
  }
  // 这里的 tab 是上方 table 表格绑定的ref值
  const divData = tab.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  // const divData2 = tab2.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  rollTimer.value = setInterval(() => {
    // + 4 是每秒向下滑动 4个像素  这块可以自己更改
    divData.scrollTop += 1
    // divData2.scrollTop += 1
    // console.log(divData.scrollTop)
    // 下方判断是滑动到底部就会自己回到最上方重新开始滑动  改动像素的话 一定要满足进入这个判断  否则滚动到底部就停了
    if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
      divData.scrollTop = 0
    }
    // if (divData2.clientHeight + divData2.scrollTop == divData2.scrollHeight) {
    //   divData2.scrollTop = 0
    // }
  }, 150)
}
onMounted(() => {
  autoRoll(false)
})
onBeforeUnmount(() => {
  autoRoll(true)
})

const tableData = ref([])
const srcList = ref([]);
const imgIndex = ref(0);
// 点击图片
const imgClicked = (src: any, srcList: any) => {
  let index = srcList.findIndex((item: any) => item === src);
  imgIndex.value = index;
};

const tableData1 = ref([]) as any
const tableData2 = ref([]) as any


// 人均产能接口返回数据
const aoppData = ref({
  cx: {
    list: [],
    xData: []
  },
  cx2: {
    list: [],
    xData: []
  },
}) as any

const getData = async () => {
  // try {
  //   // 获取表格数据
  //   let tableDataList = (await request.post<any, any>(`/lotMgt/match/show?deptType=${'成型'}&type=${'月日'}`, {
  //     pageNum: '1',
  //     pageSize: '60'
  //   })) as any
  //   tableData.value = tableDataList.dto.map((item: any) => {
  //     item.fullFlag === 'Y' ? item.fullFlag = '已满' : item.fullFlag = '未满'
  //     return item
  //   })
  // } catch (error: any) {
  //   ElMessage.error(error)
  // }
}

onMounted(async () => {
  await getData()
  getEchartsDom()
})

const getEchartsDom = () => {
  let chartDom = document.getElementById('main')
  let myChart = echarts.init(chartDom)
  let option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    legend: {
      data: ['总库存（单）', '在制库存（单）', '配套库存（单）'],
      top: 'bottom'
    },
    grid: {
      left: 60,
      right: 0,
      top: 30,
      bottom: 45
    },
    xAxis: [
      {
        type: 'category',
        data: ['总库存（单）', '在制库存（单）', '配套库存（单）'],
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        // max: 35000,
        // interval: 5000,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        type: 'bar',
        label: {
          show: true,
          position: 'top'
        },
        barWidth: '15%',
        tooltip: {
          valueFormatter: function (value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#88ADFB' },
              { offset: 1, color: '#E3ECFF' }
            ]
          )
        },
        data: [60, 38, 22]
        // data: tableData1.value
      },
    ]
  }
  option && myChart.setOption(option)

  // 第二张图
  let chartDom2 = document.getElementById('main2')
  let myChart2 = echarts.init(chartDom2)
  let option2 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    legend: {
      data: ['配套库存（单）', '满单库存（单）', '未满单库存（单）'],
      top: 'bottom'
    },
    grid: {
      left: 60,
      right: 0,
      top: 30,
      bottom: 45
    },
    xAxis: [
      {
        type: 'category',
        data: ['配套库存（单）', '满单库存（单）', '未满单库存（单）'],
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        // max: 35000,
        // interval: 5000,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        type: 'bar',
        barWidth: '15%',
        label: {
          show: true,
          position: 'top'
        },
        tooltip: {
          valueFormatter: function (value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#FF7700' },
              { offset: 1, color: 'white' }
            ]
          )
        },
        data: [22, 6, 16]
        // data: tableData2.value
      },
    ]
  }
  option2 && myChart2.setOption(option2)

}
onMounted(() => {
  function One() {
    if (location.href.indexOf('#reloaded') == -1) {
      location.href = location.href + '#reloaded'
      location.reload()
    }
  }
  One()
})
</script>

<style scoped lang="less">
.EchartsContainer {
  border-radius: 4px;
  margin-top: 10px;
  height: calc(100vh - 7vh - 40px);
  width: 100%;
}

.row {
  display: flex;
  height: 50%;
  width: 100%;

  .item {
    background-color: white;
    width: 100%;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding-bottom: 10px;
    padding: 0 10px 5px;

    // align-items: center;
    .title {
      font-weight: 900;
      font-size: 1rem;
      margin-top: 10px;
      text-align: center;
      color: black;
    }

    .echarts {
      flex: 1;
    }
  }
}

:deep(thead) {
  .is-center {
    color: white;
    background-color: #ff9940;
  }
}
</style>
